/*****************************************************************************/
/*                                                                           */
/*    Domino Operation System Driver Module                                  */
/*                                                                           */
/*    Copyright (C) 2008 Laszlo Arvai                                        */
/*                                                                           */
/*    ------------------------------------------------------------------     */
/*    drvMemoryAccess - Memory access routines (PC Emulation)                */
/*****************************************************************************/
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <krnlTypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <malloc.h>
#include <stdlib.h>
#include "drvMemoryAccess.H"
#include "sysSettings.h"

///////////////////////////////////////////////////////////////////////////////
// Module local variables
static dosByte* l_rom_file;  // ROM file storage
dosROMAddress l_string_resource = 0;

extern int g_argc;
extern char** g_argv;
#pragma warning(disable : 4996)

///////////////////////////////////////////////////////////////////////////////
// Init ROM Access
void krnlInitMemoryAccess(void)
{
	int binary_file;

	// check command line arguments
	if( g_argc != 2 )
	{
		printf( "ERROR: Invalid command line parameter.\n" );
		printf( "USAGE: DominoEmu <romfilename>\n");
		exit(-1);
	}

	// load ROM file
	binary_file = _open( g_argv[1], _O_RDONLY | _O_BINARY, _S_IREAD );
	if( binary_file != -1 )
	{
    int size;

    size = _filelength( binary_file );

		printf( "Loading ROM file '%s' (%dbytes)...\n", g_argv[1], size );

		l_rom_file = malloc( size );

		_read( binary_file, l_rom_file, size );

		_close( binary_file );
	}
	else
	{
		printf( "ERROR: Can't open ROM file: %s\n", g_argv[1] );
		exit(-1);
	}
}

///////////////////////////////////////////////////////////////////////////////
// Read byte from ROM
dosByte dosWinEmuROMReadByte( int in_address )
{
	return l_rom_file[in_address];
}

///////////////////////////////////////////////////////////////////////////////
// Read word from ROM
dosWord dosWinEmuROMReadWord( int in_address )
{
	return l_rom_file[in_address] + ((l_rom_file[in_address+1]) << 8);
}

///////////////////////////////////////////////////////////////////////////////
// Read double word from ROM
dosDWord dosWinEmuROMReadDWord( int in_address )
{
	return ((dosDWord)l_rom_file[in_address]) + 
					(((dosDWord)l_rom_file[in_address+1]) << 8) +
					(((dosDWord)l_rom_file[in_address+2]) << 16) +
					(((dosDWord)l_rom_file[in_address+3]) << 24);
}

///////////////////////////////////////////////////////////////////////////////
// Read MSB first word from ROM
dosWord dosWinEmuROMReadReverseWord( int in_address )
{
	return ((l_rom_file[in_address]) << 8) + l_rom_file[in_address+1];
}

///////////////////////////////////////////////////////////////////////////////
// String resource functions
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// Get resource string length
dosDWord krnlGetResourceStringLength( dosHandle in_string_handle )
{
  dosDWord length = 0;
  dosByte data;
  dosROMAddress address;

  address = l_string_resource + in_string_handle;

  do
  {
    data = dosROMReadByte( address++ );

    length = (length << 7) + (data & 0x7f);
  } while( data > 127 );

  return length;
}

///////////////////////////////////////////////////////////////////////////////
// Get resorce string
dosROMAddress krnlGetResourceString( dosHandle in_string_handle )
{
  dosROMAddress address;

  address = l_string_resource + in_string_handle;

  while( dosROMReadByte( address++ ) > 127 );

  return address;
}


